﻿using QFramework;
using UnityEngine;
using System;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
using System.Collections;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/GuideManager")]
    public class GuideManager : MonoBehaviour_Ex, ISingleton
    {
        private Dictionary<GuideID, Func<bool>> _dicGuides = new Dictionary<GuideID, Func<bool>>();//所有引导

        private List<GuideID> _listGuides = new List<GuideID>();//准备执行的引导

        [SerializeField]
        private BehaviorTree _behavior;

        /// <summary>
        /// 是否正在引导中
        /// </summary>
        public bool IsInGuide = false;

        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {
            _dicGuides = new Dictionary<GuideID, Func<bool>>()
            {
                 { GuideID.新手引导, () => { return false; } },
            };
            List<GuideID> hasCompleteGuides = new List<GuideID>();
            foreach (var v in _dicGuides)
            {
                if (IsGuideClear(v.Key))
                {
                    hasCompleteGuides.Add(v.Key);
                }
            }
            foreach (var v in hasCompleteGuides)
            {
                _dicGuides.Remove(v);
            }
            _listGuides = new List<GuideID>();
            IsInGuide = false;
        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static GuideManager Instance
        {
            get { return MonoSingletonProperty<GuideManager>.Instance; }
        }

        void FixedUpdate()
        {
            CheckGuide();
        }

        /// <summary>
        /// 检测引导
        /// </summary>
        void CheckGuide()
        {
            foreach (var v in _dicGuides)
            {
                if (!IsGuideClear(v.Key) && v.Value.Invoke() && !_listGuides.Contains(v.Key))
                {
                    _listGuides.Add(v.Key);
                    DoGuide();
                }
            }
        }

        /// <summary>
        /// 完成引导
        /// </summary>
        /// <param name="id"></param>
        public void CompleteGuide(GuideID id)
        {
            GameEntry.GameData.SetBool(UserData.GuideID + id, true);
            IsInGuide = false;
            if (_listGuides.Contains(id))
            {
                _listGuides.Remove(id);
                DoGuide();
            }
        }

        /// <summary>
        /// 执行引导
        /// </summary>
        void DoGuide()
        {
            if (_listGuides.Count > 0 && !IsInGuide)
            {
                IsInGuide = true;
                StartCoroutine(Ready2Guide());
            }
        }

        IEnumerator Ready2Guide()
        {
            yield return new WaitUntil(() => GameEntry.UI.GetAllPopUICount() == 0);
            if (!_dicGuides[_listGuides[0]].Invoke())
            {
                IsInGuide = false;
                if (_listGuides.Contains(_listGuides[0]))
                {
                    _listGuides.Remove(_listGuides[0]);
                    DoGuide();
                }
                yield break;
            }
            if (_behavior == null)
            {
                _behavior = gameObject.AddComponent<BehaviorTree>();
            }
            _behavior.ExternalBehavior = m_ResLoader.LoadSync<ExternalBehavior>(_listGuides[0].ToString());
        }

        /// <summary>
        /// 引导是否完成
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsGuideClear(GuideID id)
        {
            return GameEntry.GameData.GetBool(UserData.GuideID + id, false);
        }
    }

    /// <summary>
    /// 引导ID
    /// </summary>
    public enum GuideID
    {
        新手引导,
    }
}